2020-2021 Sunseeker Telemetry and Lighting System
sac_ex4_ecomp.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
62 //******************************************************************************
63 #include "Board.h"
64 #include "driverlib.h"
65 
66 uint16_t adcResult;
67 
68 int main(void)
69 {
70  //Stop WDT
71  WDT_A_hold(WDT_A_BASE);
72 
73  //Setup LED1
74  GPIO_setAsOutputPin(
75  GPIO_PORT_LED1,
76  GPIO_PIN_LED1);
77  //Set LED1 low
79  GPIO_PORT_LED1,
80  GPIO_PIN_LED1);
81 
82  /*
83  * Disable the GPIO power-on default high-impedance mode
84  * to activate previously configured port settings
85  */
86  PMM_unlockLPM5();
87 
88  //Configure reference module
89  //Enable internal 1.5V reference
90  PMM_enableInternalReference();
91  PMM_selectVoltageReference(PMM_REFVSEL_1_5V);
92  //Poll until internal reference settles
93  while(!PMM_getVariableReferenceVoltageStatus());
94 
95  //Configure OA0 and OA2 functionality
96  GPIO_setAsPeripheralModuleFunctionOutputPin(
97  GPIO_PORT_SACOA0O,
98  GPIO_PIN_SACOA0O,
99  GPIO_FUNCTION_SACOA0O);
100  GPIO_setAsPeripheralModuleFunctionInputPin(
101  GPIO_PORT_SACOA0N,
102  GPIO_PIN_SACOA0N,
103  GPIO_FUNCTION_SACOA0N);
104  GPIO_setAsPeripheralModuleFunctionInputPin(
105  GPIO_PORT_SACOA0P,
106  GPIO_PIN_SACOA0P,
107  GPIO_FUNCTION_SACOA0P);
108  GPIO_setAsPeripheralModuleFunctionOutputPin(
109  GPIO_PORT_SACOA2O,
110  GPIO_PIN_SACOA2O,
111  GPIO_FUNCTION_SACOA2O);
112  GPIO_setAsPeripheralModuleFunctionInputPin(
113  GPIO_PORT_SACOA2N,
114  GPIO_PIN_SACOA2N,
115  GPIO_FUNCTION_SACOA2N);
116  GPIO_setAsPeripheralModuleFunctionInputPin(
117  GPIO_PORT_SACOA2P,
118  GPIO_PIN_SACOA2P,
119  GPIO_FUNCTION_SACOA2P);
120 
121  //Select positive and negative pin input
122  SAC_OA_init(
123  SAC0_BASE,
124  SAC_OA_POSITIVE_INPUT_SOURCE_EXTERNAL,
125  SAC_OA_NEGATIVE_INPUT_SOURCE_PGA);
126  //Select low speed and low power mode
127  SAC_OA_selectPowerMode(
128  SAC0_BASE,
129  SAC_OA_POWER_MODE_LOW_SPEED_LOW_POWER);
130  //Set gain=5
131  SAC_PGA_setGain(
132  SAC0_BASE,
133  SAC_PGA_GAIN_BIT0 | SAC_PGA_GAIN_BIT1);
134  //Set as non-inverting PGA mode
135  SAC_PGA_setMode(
136  SAC0_BASE,
137  SAC_PGA_MODE_NONINVERTING);
138  //Enable SAC0 OA
139  SAC_OA_enable(SAC0_BASE);
140  //Enable SAC0
141  SAC_enable(SAC0_BASE);
142 
143  //Select 1.5V internal Vref as SAC2 DAC reference
144  SAC_DAC_selectRefVoltage(
145  SAC2_BASE,
146  SAC_DAC_SECONDARY_REFERENCE);
147  //Set SAC2 DAC output = 1V
148  SAC_DAC_setData(
149  SAC2_BASE,
150  0xAAA);
151  //Enable SAC2 DAC
152  SAC_DAC_enable(SAC2_BASE);
153  //Select positive and negative pin input
154  SAC_OA_init(
155  SAC2_BASE,
156  SAC_OA_POSITIVE_INPUT_SOURCE_DAC,
157  SAC_OA_NEGATIVE_INPUT_SOURCE_PGA);
158  //Select low speed and low power mode
159  SAC_OA_selectPowerMode(
160  SAC2_BASE,
161  SAC_OA_POWER_MODE_LOW_SPEED_LOW_POWER);
162  // Set OA as buffer mode
163  SAC_PGA_setMode(
164  SAC2_BASE,
165  SAC_PGA_MODE_BUFFER);
166  //Enable SAC2 OA
167  SAC_OA_enable(SAC2_BASE);
168  //Enable SAC2
169  SAC_enable(SAC2_BASE);
170 
171  /*
172  * Setup eCOMP
173  */
174  EComp_initParam param = {0};
175  //Select SAC0 and SAC2 output as eCOMP0 input
176  //Enable eCOMP input
177  param.positiveTerminalInput = ECOMP_INPUT_DEVICE_SPECIFIC_1;
178  param.negativeTerminalInput = ECOMP_INPUT_DEVICE_SPECIFIC_1;
179  param.outputFilterEnableAndDelayLevel = ECOMP_FILTER_DELAY_OFF;
180  param.invertedOutputPolarity = ECOMP_NORMAL_OUTPUT_POLARITY;
181  EComp_init(ECOMP0_BASE, &param);
182  //Enable eCOMP dual edge interrupt
183  EComp_enableInterrupt(
184  ECOMP0_BASE,
185  ECOMP_OUTPUT_INTERRUPT |
186  ECOMP_INVERTED_POLARITY_INTERRUPT);
187  //Set low power mode
188  EComp_selectPowerMode(
189  ECOMP0_BASE,
190  ECOMP_POWER_MODE_LOW_POWER_LOW_SPEED);
191  //Turn on eCOMP
192  EComp_enable(ECOMP0_BASE);
193 
194  //Enter LPM3
195  __bis_SR_register(LPM3_bits | GIE);
196  //For debug
197  __no_operation();
198 }
199 
200 // eCOMP interrupt service routine
201 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
202 #pragma vector=ECOMP0_ECOMP1_VECTOR
203 __interrupt void ECOMP0_ISR(void)
204 #elif defined(__GNUC__)
205 void __attribute__ ((interrupt(ECOMP0_ECOMP1_VECTOR))) ECOMP0_ISR (void)
206 #else
207 #error Compiler not supported!
208 #endif
209 {
210  switch(__even_in_range(CP0IV, CPIV__CPIIFG))
211  {
212  case CPIV__NONE:
213  break;
214  case CPIV__CPIFG:
215  //OA0+ input > 200mV
217  GPIO_PORT_LED1,
218  GPIO_PIN_LED1);
219  break;
220  case CPIV__CPIIFG:
221  //OA0+ input < 200mV
223  GPIO_PORT_LED1,
224  GPIO_PIN_LED1);
225  break;
226  default:
227  break;
228  }
229 }
230 
MPU_initThreeSegmentsParam param
__no_operation()
GPIO_setOutputHighOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
uint16_t adcResult
Definition: sac_ex4_ecomp.c:66
int main(void)
Definition: sac_ex4_ecomp.c:68